package cn.foolishbird.hamal.frame.queryParam.handler;

import cn.foolishbird.hamal.frame.bsassert.info.SysInfo;
import cn.foolishbird.hamal.frame.exception.ExceptionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-09-29
 */
@Slf4j
public class PickDateToLongQueryTypeHandler implements QueryTypeHandler<Long, String> {
    @Override
    public Long convert(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        date = date.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        try {
            format.parse(date).getTime();
        } catch (ParseException e) {
            log.error("pickdate日期格式错误：{}", e);
            throw ExceptionFactory.businessException(SysInfo.DATE_FORMART_ERROR);
        }
        return null;
    }
}
